home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c++
- Subject: Re: SORTing problem c++
- Date: 27 Jan 1996 23:58:27 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4eee73$504@news1.usa.pipeline.com>
- NNTP-Posting-Host: pipe5.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Jan 27, 1996 21:46:56 in article <SORTing problem c++>, 'heggie
- <heggie@pi.net>' wrote:
-
-
- >I'm used to normal c
- >just recent i switched to c++, now i am stucked
- >with a program that did work in c but doesn't
- >in c++.
- >it is a program that sorts the 'argv'command line
- >arguments array,
- >
- >what can I DO???
- >p.s. I am using borlands c++
- >p.s. thanks for reading this
- >
- >#include <stdio.h>
- >#include <stdlib.h>
- >#include <string.h>
- >
- >
- >int sort_function(char **a, char **b)
- >{
- >return( strcmp(*a, *b));
- >}
- >
- .. [ portions deleted ] ...
- >
- >qsort(argv, argc, sizeof(char*), sort_function);
- >
-
- >******************************************
- >these are the error mesages,
- >
- >
- >Error ..\SOURCES\OPDR_45.CPP 17: Cannot convert 'int (*)(char * *,char *
- >*)' to 'int (*)(const void *,const void *)'
- >Error ..\SOURCES\OPDR_45.CPP 17: Type mismatch in parameter '__fcmp' in
- >call to 'qsort(void *,unsigned int,unsigned int,int (*)(const void
- >*,const void *))'
- >
- Yup, your sort function needs to be rewritten. Here's a sample
- that I tested with MSVC++4.0, but should work on BC45 also:
-
- . your #includes plus
- #include <iostream.h>
-
- int sort_function (const void * a, const void * b)
- {
- const char * A = *((const char **)a);
- const char * B = *((const char **)b);
- return strcmp(A, B);
- }
-
- int main ()
- {
- char * bleep[7] = {"Now is", "The time", "For all", "Good men to",
- "Come", "to the aid of", "their country."};
- qsort(bleep, 7, sizeof(char*), sort_function);
- for (int i = 0; i < 7; i++)
- cout << bleep[i] << endl;
- return 0;
- }
-
- --
- Pete Grant
- Kalevi, Inc.
- Object Oriented Software Development
-